<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库编号" prop="outboundNo">
        <el-input v-model="queryParams.outboundNo" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="销售单号" prop="oddNo">
        <el-input v-model="queryParams.params.oddNo" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.params.customerName" placeholder="请输入" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出库状态" prop="outboundStatus">
        <el-select v-model="queryParams.outboundStatus" placeholder="请选择" clearable>
          <el-option key="1" label="待出库" :value="1" />
          <el-option key="2" label="已出库" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="出库时间" prop="outboundTime">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cld:statistics:export']">列表导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="ErpOutboundList" row-key="outboundId" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="50" align="center" :reserve-selection="true" bodr/>
      <el-table-column label="出库编号" align="center" prop="outboundNo" sortable width="200px;"/>
      <el-table-column label="销售单号" align="center" prop="oddNo" sortable width="200px;"/>
      <el-table-column label="供商/客户" align="center" prop="customerName"/>
      <el-table-column label="出库类型" align="center" prop="outboundType" sortable width="120px;">
        <template slot-scope="scope">
          <span v-if="scope.row.outboundType == 1">销售</span>
          <span v-if="scope.row.outboundType == 2">采购退换</span>
          <span v-if="scope.row.outboundType == 3">销售退换</span>
        </template>
      </el-table-column>
      <el-table-column label="预计出库量" align="center" prop="answerScheduledReceipt" sortable width="120px;"/>
      <el-table-column label="实际出库量" align="center" prop="outboundNumber" sortable width="120px;"/>
      <el-table-column label="出库状态" align="center" prop="outboundStatus" sortable width="120px;">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.outboundStatus == 1" type="danger">待出库</el-tag>
          <el-tag v-if="scope.row.outboundStatus == 2" type="success">已出库</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="出库人" align="center" prop="outboundPersonName" width="120px;"/>
      <el-table-column label="销售人" align="center" prop="nickName" width="120px;"/>
      <el-table-column label="联系电话" align="center" prop="phonenumber" width="120px;"/>
      <el-table-column label="出库时间" align="center" prop="outboundTime" sortable width="180px;">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outboundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否收货" align="center" prop="isHarvest" width="120px;">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isHarvest == 1" type="success">已收货</el-tag>
          <el-tag v-if="scope.row.isHarvest == 0" type="warning">未收货</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180px;">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleOutStock(scope.row)" v-if="scope.row.outboundStatus == 1" icon="el-icon-edit" v-hasPermi="['erp:ErpOutbound:edit']">出库</el-button>
          <el-button size="mini" type="text" @click="handleViewHarvest(scope.row,1)" v-if="scope.row.outboundStatus == 2" icon="el-icon-edit" v-hasPermi="['erp:ErpOutbound:edit']">收货</el-button>
          <el-button size="mini" type="text" @click="handleViewOrPrint(scope.row,1)" v-hasPermi="['erp:ErpOutbound:view']">查看</el-button>
          <el-button size="mini" type="text" @click="handleViewOrPrint(scope.row,3)" v-hasPermi="['erp:ErpOutbound:print']">打印</el-button>
          <el-button size="mini" type="text" @click="handleExportDetails(scope.row)" v-hasPermi="['erp:ErpOutbound:export']">导出</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改销售出库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :destroy-on-close="true">
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-row :gutter="15">
          <el-col :span="4">
            <el-form-item label="销售单号" prop="oddNo">
              <el-input v-model="form.oddNo" :disabled="true" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="form.customerName" :disabled="true" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="出库人员" prop="outboundPerson">
              <el-select filterable v-model="form.outboundPerson" placeholder="请选择">
                <el-option v-for="item in personOptions" :key="item.userId" :label="item.nickName"
                  :value="item.userId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="销售人" prop="nickName">
              <el-input v-model="form.nickName" :disabled="true" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="联系电话" prop="phonenumber">
              <el-input v-model="form.phonenumber" :disabled="true" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-table v-loading="loading" :data="form.detailsList" border>
            <el-table-column label="商品编号" align="center" prop="commodityNo" />
            <el-table-column label="商品名称" align="center" prop="commodityName1" />
            <el-table-column label="商品类别" align="center" prop="commodityType" />
            <el-table-column label="规格" align="center" prop="commoditySpec" />
            <el-table-column label="单位" align="center" prop="unitName1" />
            <el-table-column label="单位关系" align="center"  prop="unitRelationship"/>
            <el-table-column label="换算结果" align="center" prop="unitResult" />
            <el-table-column label="销售数量" align="center" prop="saleNumber" />
            <el-table-column label="本期出库" align="center" prop="thisIssueOut">
              <template slot-scope="scope">
                <el-input type="number" v-model="scope.row.thisIssueOut" ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="已出库" align="center" prop="realityNumber">
              <template slot-scope="scope">
                {{ !scope.row.realityNumber ? 0 : scope.row.realityNumber }}
              </template>
            </el-table-column>
            <el-table-column label="出库仓库" align="center" prop="outWareName" />
          </el-table>
        </el-row>
        <!--<el-row :gutter="15" style="padding-top: 15px;">
          <el-col :span="24">
            <el-form-item label="单据备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" :rows="4" placeholder="请输入单据备注" />
            </el-form-item>
          </el-col>
        </el-row>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">出 库</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 收货 -->
    <el-dialog :title="title" :visible.sync="harvestOpen" width="30%" append-to-body :destroy-on-close="true">
      <el-form ref="sForm" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-form-item label="收货图片" prop="commodityTypeImg">
          <image-upload v-model="form.harvestImg" @input="handleImageInput"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="harvestForm">收 货</el-button>
        <el-button @click="cancelHarvest">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 销售出库详情对话框 -->
    <el-dialog :title="title" :visible.sync="viewOpen" width="80%" append-to-body :destroy-on-close="true">
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="90px">
        <div id="printForm">
          <div class="title">
            湖南胜博清洁服务有限公司（出库单）
          </div>
          <div class="head-print">
            <div>出库单号：{{ form.outboundNo }}</div>
            <div>销售单号：{{ form.oddNo }}</div>
            <div>客户名称{{ form.customerName }}</div>
            <div>出库人员：{{ form.outboundPersonName }}</div>
            <div>出库日期：{{ parseTime(form.outboundTime, "{y}年{m}月{d}日") }}</div>
          </div>
        <right-toolbar class="no-print" :search="false" :refresh="false" :columns="columns"></right-toolbar>
        <table class="table_print">
            <tr>
              <th v-if="columns[0].visible" width="80px">序号</th>
              <th v-if="columns[1].visible">商品编号</th>
              <th v-if="columns[2].visible">商品名称</th>
              <th v-if="columns[3].visible">商品类别</th>
              <th v-if="columns[4].visible">规格</th>
              <th v-if="columns[5].visible">单位</th>
              <th v-if="columns[7].visible">出库数量</th>
              <th v-if="columns[8].visible">本期出库</th>
              <th v-if="columns[9].visible">已出库</th>
              <th v-if="columns[10].visible">出库仓库</th>
            </tr>
            <tr v-for="(item,index) in form.detailsList" :key="index">
              <td v-if="columns[0].visible">{{index + 1}}</td>
              <td v-if="columns[1].visible">{{item.commodityNo}}</td>
              <td v-if="columns[2].visible">{{item.commodityName1}}</td>
              <td v-if="columns[3].visible">{{item.commodityType}}</td>
              <td v-if="columns[4].visible">{{item.commoditySpec}}</td>
              <td v-if="columns[5].visible">{{item.unitName1}}</td>
              <td v-if="columns[7].visible">{{item.saleNumber}}</td>
              <td v-if="columns[8].visible">{{item.realityNumber}}</td>
              <td v-if="columns[9].visible">{{!item.realityNumber ? 0 : item.realityNumber }}</td>
              <td v-if="columns[10].visible">{{item.outWareName}}</td>
            </tr>
          </table>
        </div>
        <div class="head-print-2">
            <div>制单人：{{ $store.getters.nickName}}</div>
            <div style="text-align: right; padding-right: 20px;">合计数量：{{ form.sumNumber }}</div>
        </div>
        <div class="single-line-print">单据备注：{{ form.remark }}</div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-print="'#printForm'" v-if="type == 3">打印</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listErpOutbound, getErpOutbound, delErpOutbound, addErpOutbound, updateErpOutbound,updateErpOutboundHarvest } from "@/api/erp/ErpOutbound";
import { getSysUserList } from '@/api/system/user'
export default {
  name: "ErpOutbound",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售出库表格数据
      ErpOutboundList: [],
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      viewOpen: false,
      harvestOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outboundNo: null,
        outboundStatus: null,
        params: {
          oddNo: null,
          customerName: null,
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        outboundPerson: [
          { required: true, message: "出库人不能为空", trigger: "blur" }
        ]
      },
      // 用户列表
      personOptions: [],
      type: 1,
       // 打印列
       columns: [
        { key: 0, label: `序号`, visible: true },
        { key: 1, label: `商品编号`, visible: true },
        { key: 2, label: `商品名称`, visible: true },
        { key: 3, label: `商品类别`, visible: true },
        { key: 4, label: `规格`, visible: true },
        { key: 5, label: `采购单位`, visible: true },
        { key: 6, label: `胜博价`, visible: true },
        { key: 7, label: `销售数量`, visible: true },
        { key: 8, label: `本期出库`, visible: true },
        { key: 9, label: `已出库`, visible: true },
        { key: 10, label: `出库仓库`, visible: true }
      ]
    };
  },
  created() {
    this.handleGetListSysUser();
    this.getList();
  },
  methods: {
    /** 查询销售出库列表 */
    getList() {
      this.loading = true;
      listErpOutbound(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.ErpOutboundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.outboundId)
    },
    // 获取用户列表
    handleGetListSysUser() {
      getSysUserList().then(res => {
        this.personOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelHarvest() {
      this.harvestOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        outboundId: null,
        receivableId: null,
        wareId: null,
        outboundNo: null,
        outboundStatus: null,
        outboundPerson: null,
        outboundTime: null,
        answerScheduledReceipt: null,
        createId: null,
        createName: null,
        createTime: null,
        flag: null,
        outboundType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.params.customerName = null;
      this.queryParams.params.oddNo = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleViewOrPrint(row,type) {
      this.type = type;
      this.reset();
      getErpOutbound(row.outboundId).then(response => {
        this.form = response.data;
        this.form.sumNumber = 0;
        this.form.detailsList.forEach( i => {
          this.form.sumNumber += i.realityNumber
        })
        this.title = "出库单号：" + this.form.outboundNo;
        this.viewOpen = true;
      });
    },
    /** 收货按钮操作 */
    handleViewHarvest(row) {
      this.reset();
      getErpOutbound(row.outboundId).then(response => {
        this.form = response.data;
        this.form.detailsList.forEach( item => {
          item.thisIssueOut = item.saleNumber - item.realityNumber;
        })
        this.title = "出库单号：" + this.form.outboundNo;
        this.harvestOpen = true;
      });
    },
    /** 修改按钮操作 */
    handleOutStock(row) {
      this.reset();
      getErpOutbound(row.outboundId).then(response => {
        this.form = response.data;
        this.form.detailsList.forEach( item => {
          item.thisIssueOut = item.saleNumber - item.realityNumber;
        })
        this.title = "出库单号：" + this.form.outboundNo;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.outboundId != null) {
            updateErpOutbound(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addErpOutbound(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 收货按钮 */
    harvestForm() {
      this.$refs["sForm"].validate(valid => {
        if (valid) {
          if (this.form.outboundId != null) {
            updateErpOutboundHarvest(this.form).then(response => {
              this.$modal.msgSuccess("收货成功");
              this.harvestOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const outboundIds = row.outboundId || this.ids;
      this.$modal.confirm('是否确认删除销售出库编号为"' + outboundIds + '"的数据项？').then(function () {
        return delErpOutbound(outboundIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('erp/ErpOutbound/export', {
        ...this.queryParams,
        exportIds: this.ids?.join(',')
      }, `销售出库_${new Date().getTime()}.xlsx`)
    },
     // 导出
     handleExportDetails(row){
      this.download('erp/ErpOutbound/exportDetails/' + row.outboundId, {}, `销售出库_ ${row.outboundNo}.xlsx`)
    },
    // 图片上传成功
    handleImageInput(e) {
      this.form.harvestImg = e;
    },
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}
.title{
  padding: 10px;
  text-align: center;
  font-size: 32px;
  font-weight: 500;
}
.oddInfo{
  font-size: 16px;
}
.remark{
  padding-top: 10px;
  padding-bottom: 10px;
}
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
</style>
